﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Text;
using Runda.CMS.Bll; 

namespace Runda.CMS.Portal.StockManagement
{
    public partial class AccessoryReplacementCheck : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBelongto.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.AccessoryReplacementBelongto)));
                ddlCheckStatus.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.AccessoryReplacementCheckStatus)));
                ddlBelongto.Items.Insert(0, new ListItem("", ""));
                ddlCheckStatus.Items.Insert(0, new ListItem("", ""));

                BindAccessory(1);
                Calculate();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindAccessory(1);
            Calculate();
        } 

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblAccessoryPaymentStatus = e.Item.FindControl("lblAccessoryPaymentStatus") as Label;
                Label lblBelongto = e.Item.FindControl("lblBelongto") as Label;
                Label lblCheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
                LinkButton lnkbtnApprove = e.Item.FindControl("lnkbtnApprove") as LinkButton;
                HyperLink hlServiceBillNo = e.Item.FindControl("hlServiceBillNo") as HyperLink;
                Label CustomerID = e.Item.FindControl("CustomerID") as Label;
                Label CustomerName = e.Item.FindControl("CustomerName") as Label;
                DataRowView dv = e.Item.DataItem as DataRowView;

                if (dv != null)
                {
                    lblAccessoryPaymentStatus.Text = dv["AccessoryPaymentStatus"].ToString() == "0" ? "公司付" : "客户付";
                    lblBelongto.Text = EnumeratorHelper.GetFieldText(((Enumerator.AccessoryReplacementBelongto)Convert.ToInt32(dv["ReplacementBelongto"].ToString())));
                    lblCheckStatus.Text = EnumeratorHelper.GetFieldText(((Enumerator.AccessoryReplacementCheckStatus)Convert.ToInt32(dv["CheckStatus"].ToString())));

                    if (Convert.ToInt32(dv["CheckStatus"].ToString()) != (int)Enumerator.AccessoryReplacementCheckStatus.Submit)
                    {
                        lnkbtnApprove.Visible = false;
                    }
                    else
                    {
                        lnkbtnApprove.Attributes.Add("onclick", "radopen('AccessoryReplacementApprove.aspx?ReplacementID=" + dv["ReplacementID"].ToString() + "','AccessoryReplacementApprove');return false;");
                    }

                    if (Convert.ToInt32(dv["ReplacementBelongto"].ToString()) != (int)Enumerator.AccessoryReplacementBelongto.WithIn)
                    { lnkbtnApprove.Visible = false; }

                    if (Convert.ToInt32(dv["AccessoryType"].ToString()) != 1)
                    { lnkbtnApprove.Visible = false; }

                    hlServiceBillNo.Text = dv["PaperInvoicesNo"].ToString();
                    hlServiceBillNo.NavigateUrl = "../ContractFollowUp/MaintainBasic.aspx?RegisterID=" + dv["RegisterID"].ToString();
                    if (CustomerID != null)
                    {
                        CtrtCustomerEntity entity = new SystemBll().Get<CtrtCustomerEntity>(new Guid(CustomerID.Text));
                        CustomerName.Text = entity.CustomerName;
                    }
                }
            }
            
        }

        private string getWheresql()
        {
            StringBuilder sb = new StringBuilder();

            if (txtContractCodeBasic.Text.Trim() != "")
            {
                sb.Append(" and CustomerID in ( select CustomerID from vw_CustomerAllInfo where CustomerName like '%").Append(txtContractCodeBasic.Text.Trim()).Append("%')");
            }

            if (ddlCheckStatus.SelectedValue != "")
            {
                //sb.Append(" and CheckStatus >= ").Append((int)Enumerator.AccessoryReplacementCheckStatus.Submit);
                sb.Append(" and CheckStatus =").Append(ddlCheckStatus.SelectedValue);
            }

            if (ddlBelongto.SelectedValue != "")
            {
                sb.Append(" and ReplacementBelongto=").Append(ddlBelongto.SelectedValue);
            }

            if (txtAccessoryCode.Text.Trim() != "")
            {
                sb.Append(" and CompanyInstrumentCode like '%").Append(txtAccessoryCode.Text.Trim()).Append("%'");
            }

            return sb.ToString();
        }
        private void Calculate()
        {
            string Whereclause = getWheresql();
            DataRow dr = new MaintainBillBll().TotalAccessoryReplace(Whereclause).Rows[0];
            fsum1.Value = dr["custs"].ToString() + "个客户";
            fsum2.Value = dr["pjquantity"].ToString() + "个配件/易耗件";
            fsum3.Value = string.Format("{0:N}", dr["QuotePrice"]);
        }
        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "approve")
            {
                Guid MaintainID = new Guid(gridAccessoryList.MasterTableView.DataKeyValues[e.Item.ItemIndex]["MaintainID"].ToString());
               
                BindAccessory(AspNetPager1.CurrentPageIndex);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindAccessory(AspNetPager1.CurrentPageIndex);
        }

        private void BindAccessory(int pageindex)
        {
            string sqlWhere = getWheresql();

            int RecordCount = 0;
            DataTable dt = new MaintainBillBll().GetAccessortyTable(sqlWhere, "CheckDate desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            AspNetPager1.RecordCount = RecordCount;
            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindAccessory(AspNetPager1.CurrentPageIndex);
        }
    }
}
